﻿#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void printArray(int** arr, int len)
{
	for (int i = 0; i < len; ++i)
	{
		printf("3.输出一级指针数组的元素，该元素指向栈内存数据，*arr[%d]：%d\n", i, *arr[i]);
	}
}

void testSecondPointerInput()
{
	//1.堆上分配一级指针数组，都存储一级指针int*，存6个；
	int** pArray = malloc(sizeof(int*) * 6);

	//2.栈上分配数据空间；
	int a1 = 100;
	int a2 = 200;
	int a3 = 300;
	int a4 = 400;
	int a5 = 500;
	int a6 = 600;

#if 0
	pArray[0] = &a1;
	pArray[1] = &a2;
	pArray[2] = &a3;
	pArray[3] = &a4;
	pArray[4] = &a5;
	pArray[5] = &a6;
#endif

	* (pArray + 0) = &a1;
	*(pArray + 1) = &a2;
	*(pArray + 2) = &a3;
	*(pArray + 3) = &a4;
	*(pArray + 4) = &a5;
	*(pArray + 5) = &a6;

	printArray(pArray, 6);////100,...,600

	//4.释放一级指针数组的内存；
	if (pArray != NULL)
	{
		free(pArray);
		pArray = NULL;
	}
}
void testSecondPointerInputStack()
{
	//7.栈区定义数组pArray，有5个元素；
	int* pArray[5];

	//5.数据存在堆内存中；
	for (int i = 0; i < 5; ++i)
	{
		pArray[i] = malloc(4);
		*pArray[i] = 100 + i;
	}
	printArray(pArray, 5);//100,...,104

	//6.释放堆内存；
	for (int i = 0; i < 5; ++i)
	{
		if (pArray[i] != NULL)
		{
			free(pArray[i]);
			pArray[i] = NULL;
		}
	}
}

//main199：二级指针做形参输入特性
int main199()
{
	testSecondPointerInput();
	testSecondPointerInputStack();

	system("pause");
	return EXIT_SUCCESS;
}